package com.df.hitime.base;

import com.df.hitime.common.config.Config;
import com.df.hitime.common.AdminException;
import com.df.hitime.common.Constants;
import com.df.hitime.common.DateUtils;
import com.df.hitime.common.MyException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.annotation.Resource;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.util.Date;
import java.util.HashMap;

/**
 * @author liurx
 * @date 2018/11/20 11:05
 */
@Slf4j
@ControllerAdvice
public class BaseController {

    @Resource
    protected HttpServletRequest httpServletRequest;
    @Resource
    protected HttpServletResponse httpServletResponse;
    @Resource
    Config config;

    public ReturnInfo success(Object o) {
        ReturnInfo returnInfo = new ReturnInfo();
        returnInfo.setResult(true);
        returnInfo.setData(o);
        returnInfo.setCode("000");
        return returnInfo;
    }

    public ReturnInfo error(Object o) {
        ReturnInfo returnInfo = new ReturnInfo();
        returnInfo.setResult(false);
        returnInfo.setData(o);
        returnInfo.setCode("001");
        return returnInfo;
    }

    public ReturnInfo success() {
        return success("成功");
    }

    public ReturnInfo success(Object o,String code){
        ReturnInfo returnInfo = new ReturnInfo();
        returnInfo.setResult(true);
        returnInfo.setData(o);
        returnInfo.setCode("100");
        return returnInfo;
    }
    @ExceptionHandler(value = Exception.class)
    public ReturnInfo handle(Exception e) throws Exception{
        ReturnInfo returnInfo = new ReturnInfo();
        returnInfo.setResult(false);
        System.out.println(e.getMessage());
        if (e instanceof MyException) {
            String url="https://open.weixin.qq.com/connect/oauth2/authorize?appid=wxb2e9686cf91c72fb&redirect_uri="+URLEncoder.encode(config.getServerAddr()+Constants.WX_ACCESS_AUTH,"UTF-8") +"&response_type=code&scope=snsapi_userinfo&state="+e.getMessage().split("&")[1];
            returnInfo.setData(url);
            returnInfo.setCode("002");
        }else if(e instanceof AdminException){
            returnInfo.setData("登录超时，请重新登录");
            returnInfo.setCode("002");
        }else{
            returnInfo.setData("请稍后重试");
            returnInfo.setCode("001");
        }
        e.printStackTrace();
        return returnInfo;
    }

    /*public WeChatUserInfo getUserInfo() {
   String token = httpServletRequest.getHeader("token");
        if(token==null){
            token=getCookie(httpServletRequest,"token");
        }
        HashMap<String, HashMap<String, Object>> sysMap = Constants.tokenMap;
        WeChatUserInfo weChatUserInfo = null;
        if (sysMap.get(token) != null) {
            HashMap<String, Object> mm = sysMap.get(token);
            weChatUserInfo = (WeChatUserInfo) mm.get("userInfo");
            Date date = (Date) mm.get("date");
            if (new Date().getTime() >= date.getTime()) { //超时
                return null;
            }
            mm.put("date", DateUtils.addData(1));
            sysMap.put(token, mm);

        }
     *//* WeChatUserInfo weChatUserInfo = null;
      if(weChatUserInfo==null){
            weChatUserInfo=new WeChatUserInfo();
            weChatUserInfo.setOpenid("o_Mfjjt0aNkWyJo0wUGbIXgcepKc");
            weChatUserInfo.setNickName("怡红公子");
            weChatUserInfo.setProfilePicture("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83erGg9kNBBLAJicE9icJF8ofErBx0eXjIZkmPLYdibkPNYGCECsiagIcB5XfS7lHD8WrwcJkhjF0XkfwsQ/132");
        }*//*
        return weChatUserInfo;
    }*/

    public static String getCookie(HttpServletRequest request,String cookieName){

        Cookie[] cookies =  request.getCookies();
        if(cookies != null){
            for(Cookie cookie : cookies){
                if(cookie.getName().equals(cookieName)){
                    return cookie.getValue();
                }
            }
        }

        return null;
    }

}
